package com.xywang.service.bill;

import com.xywang.dao.BaseDao;
import com.xywang.dao.bill.BillDao;
import com.xywang.dao.bill.BillDaoImpl;
import com.xywang.pojo.Bill;
import com.xywang.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillServiceImpl implements BillService{
    private BillDao billDao;

    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }
    // 获取订单列表
    @Override
    public List<Bill> getBillList(Bill bill, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<Bill> billList = null;
        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillList(connection, bill,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billList;
    }
    // 获取订单总数
    @Override
    public int getBillCountService(Bill bill) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = billDao.getBillCount(connection, bill);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    // 添加订单
    @Override
    public boolean add(Bill bill) {
        boolean flag = false;
        Connection connection = null;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            i = billDao.addBill(connection, bill);
            connection.commit();
            if (i > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 删除订单
    @Override
    public boolean deleteBillById(int delId) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 关闭自动提交，开启事务
            int i = 0;
            try {
                i = billDao.deleteBill(connection, delId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            connection.commit();
            if(i>0){
                flag = true;
                System.out.println("deleteBillById删除成功");
            }else{
                System.out.println("deleteBillById删除失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback!!");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 用id获取订单
    public Bill getBillByIdService(int bid){
        Connection connection = null;
        Bill billById = null;
        try {
            connection = BaseDao.getConnection();
            billById = billDao.getBillById(connection, bid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billById;
    }
    // 修改订单
    @Override
    public boolean modify(Bill bill) {
        Connection connection = null;
        boolean flag = false;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 开启事务
            i = billDao.modify(connection, bill);
            connection.commit();
            if (i > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
}
